package com.project;

import com.project.servlet.StudentServlet;
import com.project.servlet.TestServlet;
import com.project.sevice.EmServlet;
import com.project.sevice.impl.*;
import org.apache.catalina.Context;
import org.apache.catalina.Wrapper;

import org.apache.catalina.startup.Tomcat;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
 * @Author(王思翰)
 * @Description(类的描述)
 * @Date(创建时间) 2021/7/19 13:51
 */
public class Main {
    //找到配置类，为后面servlet类中的小组件做扫描前戏
    private ApplicationContext context=new AnnotationConfigApplicationContext(MyBatisConfig.class);


    public Main() {
        Tomcat tomcat=new Tomcat();
        //开放8088端口号
        tomcat.setPort(8088);
        //引导http引擎
        tomcat.getConnector();

        //创建上下文对象
        Context ctx=tomcat.addContext("",null);

        //注册servlet
        Wrapper wrp1=tomcat.addServlet(ctx,"findAll",context.getBean(FindAllServlet.class));
        Wrapper wrp2=tomcat.addServlet(ctx,"add",context.getBean(AddServlet.class));
        Wrapper wrp3=tomcat.addServlet(ctx,"delect",context.getBean(DelectServlet.class));
        Wrapper wrp4=tomcat.addServlet(ctx,"update",context.getBean(UpdateServlet.class));
        Wrapper wrp5=tomcat.addServlet(ctx,"Gai",context.getBean(GaiServlet.class));
        Wrapper wrp6=tomcat.addServlet(ctx,"em",context.getBean(EmServlet.class));
        Wrapper wrp7=tomcat.addServlet(ctx,"stu",context.getBean(StudentServlet.class));
        //容器一旦启动，就直接把servlet初始化
        //如果是应用型servlet写不写这个无所谓，但是写框架要
        wrp1.setLoadOnStartup(1);
        wrp2.setLoadOnStartup(1);
        wrp3.setLoadOnStartup(1);
        wrp4.setLoadOnStartup(1);
        wrp5.setLoadOnStartup(1);
        wrp6.setLoadOnStartup(1);
        wrp7.setLoadOnStartup(1);
        //设置servlet请求路径
        wrp1.addMapping("/findAll");
        wrp2.addMapping("/add");
        wrp3.addMapping("/delect");
        wrp4.addMapping("/update");
        wrp5.addMapping("/Gai");
        wrp6.addMapping("/em");
        wrp7.addMapping("*.do");
        try {
            //启动tomcat
            tomcat.start();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        new Main();
    }
}
